<template>
  <div
    :id="id"
    :style="{width: `${width}px`, height: `${height}px`, margin: `34px auto`}"
    class="m-map"
  />
</template>

<script>
export default {
  props: {
    width: {
      type: Number,
      default: 300
    }, // 你是看文档写的么 我是炒的你的啊  我的key替换成我自己的了
    height: {
      type: Number,
      default: 300
    },
    point: {
      type: Array,
      default() {
        return [116.46, 39.92];
      }
    }
  },
  data() {
    return {
      id: "map",
      key: "c1bd4d60fa098a0bd9525045a843cef7"
    };
  },
  watch: {
    point(val) {
      this.map.setCenter(val);
      this.marker.setPosition(val);
    }
  },
  mounted() {
    this.id = `map${Math.random()
      .toString()
      .slice(4, 6)}`;

    window.onmaploaded = () => {
      let map = new window.AMap.Map(this.id, {
        resizeEnable: true,
        zoom: 11,
        center: this.point
      });

      this.map = map;
      window.AMap.plugin("AMap.ToolBar", () => {
        let toolBar = new window.AMap.ToolBar();

        map.addControl(toolBar);

        let marker = new window.AMap.Marker({
          icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
          position: this.point
        });

        this.marker = marker;
        marker.setMap(map);
      });
    };

    const url = `https://webapi.amap.com/maps?v=1.4.10&key=${
      this.key
    }&callback=onmaploaded`;
    let jsapi = document.createElement("script");
    jsapi.charset = "utf-8";
    jsapi.src = url;

    document.head.appendChild(jsapi);
  }
};
</script>
<style lang='scss' scoped>
.m-map {
  width: 100%;
  height: 100%;
  background-color: pink;
}
</style>
